
import { createOrUpdateApi4USetting, fetchList } from '@/views/coding/base/tmptUserSetting/api' // UserSetting

String.prototype.replaceAll = function(s1, s2) {
  return this.replace(new RegExp(s1, "gm"), s2);
}

export default {
    // save to remote server
    saveRemote(key, value) {
        //console.log( value)
        //console.log(typeof value)

        const data = {}
        data.name = key

        if (typeof value === "boolean") {
            data.value = value
        }
        else if (typeof value === "string") {
            data.value = value
        }
        else if (typeof value === "number") {
            data.value = value
        }
        else if (typeof value === "object") {
            data.value = JSON.stringify(value) // .replaceAll("\"","\\\"")
        }
        createOrUpdateApi4USetting(data).then( result => {
            console.log('saveRemote: '+result)
        })
    },
    // load all user setting after login
    loadByUserId(userId) {
        const query = {page: 1, limit: 200000, createmanId: userId}
        return fetchList(query).then( r => {
            if (!r.success) {
                return r
            }
            const list = r.map.rows
            list.forEach(item => {
                const name = item.name;
                const value = item.value;
                window.localStorage.setItem(name, value)
                /*
                if (value === 'true' || value === 'false' || !isNaN(value)) {
                    window.localStorage.setItem(name, value)
                } else {
                    //console.log(value)
                    const value1 = value.replaceAll("\\\\\"", "\"")
                    window.localStorage.setItem(name, value)
                }*/
            })
            return r
        })
    },

    save(key, obj, isRemote) {
        let value
        if (typeof obj === "boolean") {
          value = obj
        }
        else if (typeof obj === "string") {
          value = obj
        }
        else if (typeof obj === "number") {
          value = obj
        }
        else if (typeof obj === "object") {
          value = JSON.stringify(obj)
        }
        window.localStorage.setItem(key, value)

        if (isRemote) {
            this.saveRemote(key, obj)
        }
    },
    readArr(key) {
        return JSON.parse(window.localStorage.getItem(key) || '[]')
    },

    readObj(key) {
        return JSON.parse(window.localStorage.getItem(key) || '{}')
    },

    readStr(key) {
        return window.localStorage.getItem(key) || ''
    },

    readBool(key, b) {
        if (b === undefined) {
            b = true
        }else{
            b = false
        }
        return JSON.parse(window.localStorage.getItem(key) || b)
    },


    remove(key) {
      window.localStorage.removeItem(key)
    },

    /**
     * 从local storage中，读取列表头宽度，undefined则返回默认值
     * @param prop
     * @param defaultValue
     * @returns {*}
     */
    getColumnWidthFromStorage(prop, defaultValue, ColumnShowListWidthKey) {
        const columnWidthJson = this.readObj(ColumnShowListWidthKey)
        let width = columnWidthJson[prop]
        if (width === undefined) {
          width = defaultValue
        }
        return width
    }

}
